#!/usr/bin/env python
# encoding: utf-8
"""
GameListView.py

Created by Steve Streza on 2008-09-20.
"""

from Tkinter import *
from GameListItemView import GameListItemView
from Observer import Observer
from BreakerMove import BreakerMove
from MakerMove import MakerMove

class GameListView(Canvas,Observer):
        """contains a list of all breaker guesses and maker responses
           (which are stored in pairs in GameListItemView)
        """
        
	def __init__(self, master,height, **kv):
                """constructor."""
		Canvas.__init__(self,master,kv,height=height)
		
		self.width = 488
		self.height = height
		#self.emptyFrame = Frame(self, width=300, height=self.height, bg="red")

		self.breakerRows = []
		self.makerRows = []
		self.lastMove = None
		
		#self.emptyFrame.grid(row=0, rowspan=10)
		
		pass
		
	def addBreakerMove(self,breakerMove):
		"""add a breaker move to the list"""
		if len(self.breakerRows) == len(self.makerRows):
			self.breakerRows.append(breakerMove)
			self.lastMove = breakerMove
			pass
		else:
			#THIS SHOULD NEVER HAPPEN
			print "ERROR: Trying to add breaker move out of place. ", len(self.breakerRows), " ", len(self.makerRows)
			pass
		pass
		
	def addMakerMove(self,makerMove):
		"""add a maker move to the list"""
		if len(self.breakerRows) - 1 == len(self.makerRows):
			self.makerRows.append(makerMove)
			self.lastMove = makerMove
			pass
		else:
			#THIS SHOULD NEVER HAPPEN
			print "ERROR: Trying to add maker move out of place. ", len(self.breakerRows), " ", len(self.makerRows)
			pass
		pass
		
	def rectOfRowAndColumn(self,row,column):
		"""return the rectangle area"""
		row = (10-len(self.breakerRows)) + row
		
		x = column*150
		y = row*48
		width = x + 150
		height = y + 48
		
		return {'x': x, 'y': y, 'width': width, 'height': height}
		
	def rectOfItemInRect(self,item,rect):
		"""return the rect in the item in the rect"""
		x = rect['x'] + (25 * item) + (6 * (item + 1))
		y = rect['y']
		width = x + 25
		height = y + 25

		return {'x': x, 'y': y, 'width': width, 'height': height}
		
	def rectToString(self,rect):
		"""retrun a string representation of the rect"""
		return ""+str(rect['x'])+"x"+str(rect['y'])+", "+str(rect['width'])+"x"+str(rect['height'])
		
	def rebuildViewWithStack(self,stack):
		"""reload moves from stack"""
#		self.configure(height=self.height)
#		self.emptyFrame.configure(height=(self.height - (index * 48)))
#		self.emptyFrame.grid(row=0,rowspan=(10-index),ipadx=0,ipady=0,padx=0,pady=0)

		self.delete(ALL)
		index = 0
#		self.config(width=self.width,height=self.height)
		
#		print "Stack length", str(len(stack))
		
#		for breaker in self.breakerRows:
		while index < len(stack):
			row = int(index/2)
			if index % 2 == 0:
				breaker = stack[index]
#				print "Breaker at " + str(index) + " (" + str(row) + "): " + breaker.to_string()
				rect = self.rectOfRowAndColumn(row,0)

				self.create_rectangle(rect['x'], rect['y'], rect['width'], rect['height'])
				pieces = breaker.get_pieces()
				pieceIndex = 0
				while pieceIndex < len(pieces):
					piece = pieces[pieceIndex]
					ovalRect = self.rectOfItemInRect(pieceIndex,rect)
					fillColor = breaker.get_color_name(piece)
					if fillColor == "None":
						fillColor = "gray"
					self.create_oval(ovalRect['x'], ovalRect['y'], ovalRect['width'], ovalRect['height'], fill=fillColor)
#					print "Drawing oval for " + str(pieceIndex) + " " + fillColor
					pieceIndex += 1
			else:
				maker = stack[index]
#				print "Maker at " + str(index) + " (" + str(row) + "): " + maker.to_string()
				rect = self.rectOfRowAndColumn(row,1)

				self.create_rectangle(rect['x'], rect['y'], rect['width'], rect['height'])

				pieces = maker.get_pieces()
				pieceIndex = 0
				for piece in pieces:
					ovalRect = self.rectOfItemInRect(pieceIndex,rect)
					fillColor = maker.get_color_name(piece)
					if fillColor == "None":
						fillColor = "gray"
					self.create_rectangle(ovalRect['x'], ovalRect['y'], ovalRect['width'], ovalRect['height'], fill=fillColor)
#					print "Drawing rect for " + str(pieceIndex) + " " + fillColor
					pieceIndex += 1

			index += 1
	
	def setup_view(self):
		"""set up the view"""
#		self.canvas.config(bg="green")
		pass

	def update(self,game,value):
		"""update the component"""
		move = game.get_last_move()
#		print "Gots us a move. ",move
		if isinstance(move,BreakerMove):
			self.addBreakerMove(move)
		else:
			self.addMakerMove(move)
		self.rebuildViewWithStack(game.get_move_stack())

	def reset_rows(self):
                """delete all of the current moves"""
                self.breakerRows = []
		self.makerRows = []
		self.lastMove = None
		self.delete(ALL)
